package com.superman.supermarket.client.view.categories;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.MobileUtils;
import com.superman.mvpframe.client.widget.toggle.ToggleItem;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent.ToggleSelectEventHandler;
import com.superman.supermarket.client.composite.categories.second.SecondModule;
import com.superman.supermarket.client.composite.categories.third.ThirdModule;
import com.superman.supermarket.client.composite.categories.top.TopCateItem;
import com.superman.supermarket.client.composite.categories.top.TopCateListModule;
import com.superman.supermarket.client.composite.common.nodata.NoDataModule;

/**
 * 商品分类ui实现类
 * 
 * @author superman
 * @version 2018年8月2日下午5:48:20
 */
public class SKUCategoriesViewImpl extends Composite implements SKUCategoriesView {

	interface SKUCategoriesViewImplUiBinder extends UiBinder<Widget, SKUCategoriesViewImpl> {
	}

	private static SKUCategoriesViewImplUiBinder uiBinder = GWT.create(SKUCategoriesViewImplUiBinder.class);

	@UiField
	TopCateListModule leftpanel;
	private SKUCategoriesViewPresenter presenter;

	@UiField
	FlowPanel rightpanel;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月13日 下午11:16:13
	 */
	public SKUCategoriesViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		leftpanel.addToggleSelectEventHandler(new ToggleSelectEventHandler() {

			@Override
			public void onSelected(ToggleSelectEvent event) {
				ToggleItem toggleItem = event.getToggleItem();
				if (toggleItem instanceof TopCateItem) {
					TopCateItem topCateItem = (TopCateItem) toggleItem;
					JSONObject jo = topCateItem.getJOData();
					String cateId = jo.get("CATALOG_ID").isString().stringValue();
					presenter.getChildCateListByCateId(cateId);

					// 将点击的目标移动到可视屏幕的中间位置
					// 当前滚动条的位置
					int wrapperScrollTop = leftpanel.getElement().getScrollTop();
					// 当前点击的区域位于屏幕的绝对唯一
					int targetAbsTop = topCateItem.getElement().getAbsoluteTop() + topCateItem.getOffsetHeight() / 2;
					// 需要移动到屏幕中间的差值
					int abs = targetAbsTop - Window.getClientHeight() / 2;
					// 计算后的滚动条位置
					int top = wrapperScrollTop + abs;
					// 滚动
					leftpanel.getElement().setScrollTop(top);

				}
			}
		});
	}

	@Override
	public void init() {
		leftpanel.setHeight(Window.getClientHeight() - MobileUtils.px2(90) + "px");
		rightpanel.setHeight(Window.getClientHeight() - MobileUtils.px2(90) + "px");

	}

	@Override
	public void refreshChildCategoryList(JSONArray ja) {
		rightpanel.clear();

		FlowPanel flowPanel = new FlowPanel();

		for (int i = 0; i < ja.size(); i++) {
			JSONObject jo = ja.get(i).isObject();
			if (jo.get("HAS_SUB").isNumber().doubleValue() > 0) {
				SecondModule secondModule = new SecondModule();
				secondModule.setData(jo);
				flowPanel.add(secondModule);
			} else {
				ThirdModule thirdModule = new ThirdModule();
				thirdModule.setData(jo);
				flowPanel.add(thirdModule);
			}
		}

		rightpanel.add(flowPanel);
	}

	@Override
	public void refreshTopCategoryList(JSONArray ja) {
		leftpanel.clear();
		for (int i = 0; i < ja.size(); i++) {
			TopCateItem topCateItem = new TopCateItem();
			topCateItem.setJOtData(ja.get(i).isObject());
			leftpanel.add(topCateItem);
		}
		leftpanel.selectByIndex(0);
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		this.presenter = (SKUCategoriesViewPresenter) presenter;
	}

	@Override
	public void showNoChildCategoryList() {
		rightpanel.clear();
		rightpanel.add(new NoDataModule());
	}

}
